package models

import (
	"github.com/go-xorm/xorm"
)

type OpenIns struct {
	ID     int    `xorm:"id pk autoincr"`
	OpenID string `xorm:"openid"`
	Title  string `xorm:"title"`
}

func (OpenIns) TableName() string {
	return "OpenIns"
}

// TODO Insert into OpenIns
func OpenInsInsert(db *xorm.Engine, openid string, title string) error {
	loggerl.Debugf("OpenInsInsert: %s %s", openid, title)
	_, err := db.Exec("INSERT INTO openins (openid, title) VALUES (?, ?)", openid, title)
	if err != nil {
		return err
	}
	return nil
}

// TODO create table openins sqlserver 2008
func OpenInsCreateTable(db *xorm.Engine) error {
	loggerl.Debugf("OpenInsCreateTable")
	_, err := db.Exec("CREATE TABLE OpenIns (id INT IDENTITY(1,1) PRIMARY KEY, openid VARCHAR(255), title VARCHAR(255))")
	if err != nil {
		return err
	}
	return nil
}

// 判断表OpenIns 是否存在
func OpenInsTableExists(db *xorm.Engine) (bool, error) {
	loggerl.Debugf("OpenInsTableExists")
	var exists bool
	result, err := db.QueryString("SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'OpenIns'")
	if err != nil {
		return false, err
	}
	if len(result) > 0 {
		exists = true
	} else {
		exists = false
	}
	return exists, nil
}
